package com.resource.effect
{
	import com.resource.animation.Animation;
	import com.resource.animation.AnimationCacher;
	import com.resource.animation.AnimationError;
	import com.resource.animation.AnimationEvent;
	
	import flash.geom.Point;

	/**
	 * 自定义动画格式
	 * 做缓存
	 * 
	 * 能保持帧率
	 */
	public class AnimationEffect extends Animation
	{
		private var _loop:Boolean = false; // 是否循环播放
		private var _url:String;
		
		public function AnimationEffect(url:String, loop:Boolean = false)
		{
			super();
			this._url = url;
			this._loop = loop;
			endCallback = onEnd;
		}
		
		public override function play():void{
			var animaCacher:AnimationCacher = AnimationCacher.getInstance();
			var data:Object = animaCacher.getAnimation(_url);
			if(data){
//				this.frameList = data.frameList;
//				this.position = data.position;
//				this.name = data.name;
//				this.hpBar = data.hpBar;
//				this.ride = data.ride;
//				super.gotoAndPlay(0);
			}else{
				animaCacher.addEventListener(_url, onLoaded);
				animaCacher.addEventListener(_url + "error", onError);
				animaCacher.load(_url);	
			}
		}
		
		private function onLoaded(event:AnimationEvent):void{
			var animaCacher:AnimationCacher = AnimationCacher.getInstance();
			animaCacher.removeEventListener(_url, onLoaded);
			animaCacher.removeEventListener(_url, onError);
			
			var data:Object = event.data;
//			this.frameList = data.frameList;
//			this.position = data.position;
//			this.name = data.name;
//			this.hpBar = data.hpBar;
//			this.ride = data.ride;
//			super.gotoAndPlay(0);
		}
		
		private function onEnd():void{
			if(!_loop){
				super.stop();
			}
		}
		
		private function onError(event:AnimationError):void{
			var animaCacher:AnimationCacher = AnimationCacher.getInstance();
			animaCacher.removeEventListener(_url, onLoaded);
			animaCacher.removeEventListener(_url, onError);
		}
		
		public function get url():String
		{
			return _url;
		}

		public function set url(value:String):void
		{
			_url = value;
		}

		public function get loop():Boolean
		{
			return _loop;
		}
		
		public function set loop(value:Boolean):void
		{
			_loop = value;
		}
	}
}